<template>
  <div>
    <el-backtop :right="100" :bottom="100">
      <div
          style="
        height: 100%;
        border-radius: 10px;
        width: 100%;
        background-color: #808080;
        box-shadow: var(--el-box-shadow-lighter);
        text-align: center;
        line-height: 40px;
        color: #f6f6f6; ">
        ▲
      </div>
    </el-backtop>
    <CarouselComponent :imageUrls="imageUrls" class="fade-in"/>
    <div class="list_title w1050 lf">
      <h2>【{{ getType(currentName) }}】</h2>
      <div class="list" ref="listContainer">
        <el-card
            v-for="(art, index) in listArt"
            :key="index"
            :class="getAnimationClass(index)"
            @click="toArtDetail(art)"
            class="card"
        >
          <template #header>
            <img
                :src="baseURL+`/file/download?filename=` + art.index + `.jpeg`"
                class="art-image"
                style="width: 100%; height: 200px"
            />
          </template>
          <template #default>
            <div class="art-name">{{ art.name }}</div>
          </template>
        </el-card>
        <div v-if="loading" class="loading">Loading...</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import CarouselComponent from "@/components/Carousel/CarouselComponent.vue";
import {onMounted, ref, watch, nextTick} from "vue";
import {useRoute} from "vue-router";
import {getArtAPi} from "@/api/art";
import {onUnmounted} from 'vue';
import router from "@/router";
import {getType} from "@/utils/util";
import {baseURL} from "@/api/request";

// 路由和状态
const route = useRoute();
const currentName = ref("A" as any);//当前分类名
const listArt = ref([] as any[]);//艺术列表
const loading = ref(false);//loading文字是否展示
const page = ref(1);//页码
const pageSize = 8;  // 每次加载的条数
const hasMore = ref(true);

// 获取数据的函数
async function getData(loadMore = false) {
  if (loading.value) return; // 防止重复加载
  loading.value = true;
  getArtAPi({type: currentName.value}).then((res: any) => {
    if (res.data.length < pageSize) {
      hasMore.value = false; // 数据少于 pageSize，说明没有更多数据了
    }
    if (loadMore) {
      listArt.value = [...listArt.value, ...res.data];
    } else {
      listArt.value = res.data;
    }
    page.value += 1;
  }).finally(() => {
    loading.value = false;
  });
}

// 监听路由参数变化
watch(
    () => route.query.name,
    (newName) => {
      currentName.value = newName as string;
      page.value = 1;  // 重置页码
      listArt.value.length = 0
      hasMore.value = true; // 重置更多数据标志
      getData();
    }
);

function toArtDetail(id: any) {
  router.push({
    path: '/artDetail',
    query: {...id, img: baseURL+`/file/download?filename=` + id.index + `.jpeg`}
  });
}

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      entry.target.classList.add('in-view');
      observer.unobserve(entry.target);
    }
  });
});

onMounted(() => {
  currentName.value=route.query.name
  getData(); // 初始加载数据
  window.addEventListener('scroll', handleScroll) // 监听页面滚动
});

onUnmounted(() => {
  const container = document.querySelector('.list');
  if (container) {
    container.removeEventListener('scroll', handleScroll);
  }
  observer.disconnect();
});

// 示例图片 URL 地址数组
const imageUrls = ref([
  {url: baseURL+'/file/download?filename=A.jpg', name: "A"},
  {url: baseURL+'/file/download?filename=B.jpg', name: "B"},
  {url: baseURL+'/file/download?filename=C.jpg', name: "C"},
  {url: baseURL+'/file/download?filename=D.jpg', name: "D"},
  {url: baseURL+'/file/download?filename=E.jpg', name: "E"},
  {url: baseURL+'/file/download?filename=F.jpg', name: "F"},
  {url: baseURL+'/file/download?filename=G.jpg', name: "G"},
  {url: baseURL+'/file/download?filename=H.jpg', name: "H"},
] as any);

// 获取动画类名
function getAnimationClass(index: number) {
  const column = index % 4;
  if (column === 0 || column === 1) {
    return ['card', 'left-to-right'];
  } else {
    return ['card', 'right-to-left'];
  }
}

function handleScroll() {
  let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  // console.log(scrollTop, '滚动距离')
  const container = document.querySelector('.list') as HTMLElement;
  if (container) {
    const scrollTop = container.scrollTop;
    const scrollHeight = container.scrollHeight;
    const clientHeight = container.clientHeight;
    if (scrollTop + clientHeight >= scrollHeight - 5 && hasMore.value) {

      nextTick(() => {
        // 重新观察新加载的 el-card 元素
        document.querySelectorAll('.list > .el-card:not(.in-view)').forEach(el => {
          observer.observe(el);
        });
      });
    }
  }
}
</script>

<style scoped>
.to-top {
  position: fixed;
  z-index: 50;
  right: 10px;
  bottom: 120px;
}

.to-top-button {
  padding: 20px;
  font-size: 30px;
  border-radius: 10px;
  background-color: #808080;
  color: white;
  border: #ffffff;
}

.list_title {
  margin-top: 40px;
}

.list {
  margin-top: 30px;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 260px);
  grid-gap: 20px;
  justify-content: center;
}

.card {
  margin: 0; /* 由 grid-gap 控制间距 */
}

.art-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading {
  text-align: center;
  margin: 20px;
  font-size: 16px;
  color: #999;
}

.left-to-right {
  opacity: 0;
  transform: translateX(-100%);
  transition: transform 1s ease-in, opacity 1s ease-in;
}

.right-to-left {
  opacity: 0;
  transform: translateX(100%);
  transition: transform 1s ease-in, opacity 1s ease-in;
}

.in-view {
  opacity: 1;
  transform: translateX(0);
}
</style>


<!--
<template>
  <div>
    <el-backtop :right="100" :bottom="100">
      <div
          style="
        height: 100%;
        border-radius: 10px;
        width: 100%;
        background-color: #808080;
        box-shadow: var(&#45;&#45;el-box-shadow-lighter);
        text-align: center;
        line-height: 40px;
      color: #f6f6f6; ">
        ▲
      </div>
    </el-backtop>
    <CarouselComponent :imageUrls="imageUrls" class="fade-in"/>
    <div class="list_title w1050 lf">
      <h2>【{{ getType(currentName) }}】</h2>
      <div class="list" ref="listContainer">&lt;!&ndash;            :class="['card', 'fade-in']"&ndash;&gt;
        <el-card
            v-for="(art, index) in listArt"
            :key="index"
            :class="getAnimationClass(index)"
            @click="toArtDetail(art)"
            style="width: 260px; margin: 20px;"
        >
          <template #header>
            <img
                :src="`http://123.56.131.61:8000/file/download?filename=` + art.index + `.jpeg`"
                class="art-image"
                style="width: 100%; height: 200px"
            />
          </template>
          <template #default>
            <div class="art-name">{{ art.name }}</div>
          </template>
        </el-card>
        <div v-if="loading" class="loading">Loading...</div>
      </div>
    </div>
  </div>
</template>


<script lang="ts" setup>
import CarouselComponent from "@/components/Carousel/CarouselComponent.vue";
import {onMounted, ref, watch, nextTick} from "vue";
import {useRoute} from "vue-router";
import {getArtAPi} from "@/api/art";
import {onUnmounted} from 'vue';
import router from "@/router";
import {getType} from "@/utils/util";

// 路由和状态
const route = useRoute();
const currentName = ref("");//当前分类名
const listArt = ref([] as any[]);//艺术列表
const loading = ref(false);//loading文字是否展示
const page = ref(1);//页码
const pageSize = 8;  // 每次加载的条数
const hasMore = ref(true);

// 获取数据的函数
async function getData(loadMore = false) {
  if (loading.value) return; // 防止重复加载
  loading.value = true;

  getArtAPi({type: currentName.value}).then((res: any) => {
    if (res.data.length < pageSize) {
      hasMore.value = false; // 数据少于 pageSize，说明没有更多数据了
    }
    if (loadMore) {
      listArt.value = [...listArt.value, ...res.data];
    } else {
      listArt.value = res.data;
    }
    page.value += 1;
  }).finally(() => {
    loading.value = false;
  });


}

// 监听路由参数变化
watch(
    () => route.query.name,
    (newName) => {
      currentName.value = newName as string;
      page.value = 1;  // 重置页码
      listArt.value.length = 0
      hasMore.value = true; // 重置更多数据标志
      getData();
    }
);

function toArtDetail(id: any) {
  router.push({
    path: '/artDetail',
    query: {...id, img: `http://123.56.131.61:8000/file/download?filename=` + id.index + `.jpeg`}
  });
}


const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      entry.target.classList.add('in-view');
      observer.unobserve(entry.target);
    }
  });
});

onMounted(() => {
  getData(); // 初始加载数据
  window.addEventListener('scroll', handleScroll) // 监听页面滚动
});

onUnmounted(() => {
  const container = document.querySelector('.list');
  if (container) {
    container.removeEventListener('scroll', handleScroll);
  }
  observer.disconnect();
});

// 示例图片 URL 地址数组
const imageUrls = ref([
  {url: 'http://123.56.131.61:8000/file/download?filename=A.jpg', name: "A"},
  {url: 'http://123.56.131.61:8000/file/download?filename=B.jpg', name: "B"},
  {url: 'http://123.56.131.61:8000/file/download?filename=C.jpg', name: "C"},
  {url: 'http://123.56.131.61:8000/file/download?filename=D.jpg', name: "D"},
  {url: 'http://123.56.131.61:8000/file/download?filename=E.jpg', name: "E"},
  {url: 'http://123.56.131.61:8000/file/download?filename=F.jpg', name: "F"},
  {url: 'http://123.56.131.61:8000/file/download?filename=G.jpg', name: "G"},
  {url: 'http://123.56.131.61:8000/file/download?filename=H.jpg', name: "H"},
] as any);

// 获取动画类名
function getAnimationClass(index: number) {
  const column = index % 4;
  if (column === 0 || column === 1) {
    return ['card', 'left-to-right'];
  } else {
    return ['card', 'right-to-left'];
  }
}

function handleScroll() {
  let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  console.log(scrollTop, '滚动距离')
  const container = document.querySelector('.list') as HTMLElement;
  if (container) {
    const scrollTop = container.scrollTop;
    const scrollHeight = container.scrollHeight;
    const clientHeight = container.clientHeight;
    if (scrollTop + clientHeight >= scrollHeight - 5 && hasMore.value) {

      nextTick(() => {
        // 重新观察新加载的 el-card 元素
        document.querySelectorAll('.list > .el-card:not(.in-view)').forEach(el => {
          observer.observe(el);
        });
      });
    }
  }
}


</script>


<style scoped>
.to-top {
  position: fixed;
  z-index: 50;
  right: 10px;
  bottom: 120px;
}

.to-top-button {
  padding: 20px;
  font-size: 30px;
  border-radius: 10px;
  background-color: #808080;
  color: white;
  border: #ffffff;
}

.list_title {
  margin-top: 40px;
}

.list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  overflow-y: auto;
  justify-content: center;
  overflow: hidden;
}

.art-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading {
  text-align: center;
  margin: 20px;
  font-size: 16px;
  color: #999;
}

.left-to-right {
  opacity: 0;
  transform: translateX(-100%);
  transition: transform 1s ease-in, opacity 1s ease-in;
}

.right-to-left {
  opacity: 0;
  transform: translateX(100%);
  transition: transform 1s ease-in, opacity 1s ease-in;
}

.in-view {
  opacity: 1;
  transform: translateX(0);
}
</style>
-->
